<?php


namespace app\common\service\lib\tencentCloud;


class CosLive
{
    /**
     * Author:YangXinPeng
     * 获取腾讯云直播推流地址
     * @param $domain
     * @param $streamName
     * @param null $key
     * @param null $time
     * @return string
     */
    public function getPushUrl($domain, $streamName, $key = null, $time = null)
    {
        if ($key && $time) {
            $txTime = strtoupper(base_convert(strtotime($time), 10, 16));
            $txSecret = md5($key . $streamName . $txTime);
            $ext_str = "?" . http_build_query(array(
                    "txSecret" => $txSecret,
                    "txTime" => $txTime
                ));
        }
        return "rtmp://" . $domain . "/live/" . $streamName . (isset($ext_str) ? $ext_str : "");
    }


    /**
     * Author:YangXinPeng
     * 获取腾讯云生成播流地址
     * @param $domain
     * @param $streamName
     * @param null $key
     * @param null $time
     * @return string
     */
    public function getPullUrl($domain, $streamName, $key = null, $time = null)
    {
        if ($key && $time) {
            //将十进制转到16进制
            $txTime = strtoupper(base_convert(strtotime($time), 10, 16));
            $txSecret = md5($key . $streamName . $txTime);
            //生成通过 URL 编码后的查询字符串
            $ext_str = "?" . http_build_query(array(
                    "txSecret" => $txSecret,
                    "txTime" => $txTime
                ));
        }
        return "https://" . $domain . "/live/" . $streamName . ".m3u8" . (isset($ext_str) ? $ext_str : "");
    }
}